package zhongbo.java.air.dictionary.web.controller;

import cn.hutool.core.date.DateTime;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zhongbo.java.air.dictionary.exception.BusinessException;
import zhongbo.java.air.dictionary.exception.ExceptionCode;
import zhongbo.java.air.dictionary.model.AirCraft;
import zhongbo.java.air.dictionary.model.enums.AirStatus;
import zhongbo.java.air.dictionary.service.IAirCraftService;
import zhongbo.java.air.dictionary.vo.PageVo;
import zhongbo.java.air.dictionary.vo.RestResultVo;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * 说明：
 * 飞机基础信息操作的相关接口
 *
 * @author 钟波dellNotebook
 * @since 2025年03月31日 星期一 08:30:28
 */
@RestController
@RequestMapping("/aircraft")
@Api(tags ="飞机基础信息操作的相关接口")
public class AirCraftController {
    @Autowired
    IAirCraftService airCraftService;

    @PostMapping("add")
    @ApiOperation("新增飞机")
    RestResultVo<AirCraft> add(AirCraft airCraft){
        airCraft.setCreateTime(DateTime.now());
        airCraft.setUpdateTime(DateTime.now());
        airCraft.setStatus(AirStatus.ACTIVE);
        RestResultVo<AirCraft> ResultVo = CheckData(airCraft);
        AirCraft data = ResultVo.getData();
        if (data == null || data.equals("")){
            String massage = ResultVo.getMassage();
            return RestResultVo.failed(massage);
        }
        int addAir = airCraftService.addAir(data);
        if (addAir <= 0){
          return  RestResultVo.failed("添加失败");
        }
        return RestResultVo.ok(data);
    }
    private RestResultVo<AirCraft> CheckData(AirCraft airCraft) {
        if (airCraft == null && airCraft.equals("")){
            return RestResultVo.failed("数据传输异常");
        }
        String icaoCode = airCraft.getIcaoCode();
        String registration = airCraft.getRegistration();
        String aircraftType = airCraft.getAircraftType();
        if (!icaoCode.matches("^[0-9]{1,6}$")){
            return RestResultVo.failed("ICAO地址码只能是1~6位的数字");
        }
        if (!registration.matches( "^[A-Z][-][0-9]{1,}$")){
            return RestResultVo.failed("注册号格式不规范(例:B-1234)");
        }
        if (!aircraftType.matches("^[A-Z][0-9]{1,3}[-][0-9]{1,3}$")){
            return RestResultVo.failed("机型格式不规范(例:B737-800)");
        }
        return RestResultVo.ok(airCraft);
    }

    @GetMapping("delete")
    @ApiOperation("删除信息")
    RestResultVo<AirCraft> delete(String id){
        if (id == null && id.equals("")) {
            RestResultVo.failed("id为空");
        }
        AirCraft airCraft = airCraftService.selectById(id);
        airCraftService.delete(id);
        return RestResultVo.success("删除成功! 原数据：" + airCraft);
    }
    @PostMapping("update")
    @ApiOperation("更新信息")
    RestResultVo<AirCraft> update(AirCraft airCraft){
        if (airCraft == null && airCraft.equals("")){
            RestResultVo.failed("数据信息为空");
        }
        airCraft.setUpdateTime(DateTime.now());
        airCraft.setStatus(AirStatus.RETIRED);
        int update = airCraftService.update(airCraft);
        if (update <= 0){
            return RestResultVo.failed("更新失败");
        }
        return RestResultVo.success("更新成功");
    }
    @GetMapping("datil")
    @ApiOperation("飞机详细信息")
    RestResultVo<AirCraft> selectById(String id){
        if (id == null && id.equals("")){
            throw new BusinessException(ExceptionCode.ID_NULL);
        }
        AirCraft airCraft = airCraftService.selectById(id);
        if (airCraft == null && airCraft.equals("")){
            return RestResultVo.failed("查询失败！");
        }
        return RestResultVo.ok(airCraft);
    }
    @GetMapping("page")
    @ApiOperation("分页查询")
    public RestResultVo<PageVo<AirCraft>> page(String airline, Integer pageNumber, Integer pageSize){
        PageVo<AirCraft> pageVo = airCraftService.page(airline,pageNumber,pageSize);
        return RestResultVo.ok(pageVo);
    }
}
